define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/feiyu/xiansuo/index' + location.search,
                    add_url: 'zhongzhuan/feiyu/xiansuo/add',
                    edit_url: 'zhongzhuan/feiyu/xiansuo/edit',
                    del_url: 'zhongzhuan/feiyu/xiansuo/del',
                    multi_url: 'zhongzhuan/feiyu/xiansuo/multi',
                    import_url: 'zhongzhuan/feiyu/xiansuo/import',
                    table: 'ec_feiyu_xiansuo',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: '分配类型', searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.status},
                        {field: 'is_enable_assign', title: '能分配', searchList: {"0":"不可分配","1":"可分配"}, formatter: Table.api.formatter.status},
                        {field: 'clue_id', title: __('Clue_id'), operate: 'LIKE'},
                        {field: 'advertiser_id', title: __('Advertiser_id'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'telphone', title: __('Telphone'), operate: 'LIKE'},
                        {field: 'ad_id', title: __('Ad_id'), operate: 'LIKE'},
                        {field: 'creative_id', title: __('Creative_id'), operate: 'LIKE'},
                        {field: 'site_id', title: __('Site_id'), operate: 'LIKE'},
                        {field: 'external_url', title: __('External_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
//                        {field: 'clue_type', title: __('Clue_type')},
//                        {field: 'clue_state', title: __('Clue_state')},
                        {field: 'clue_state_name', title: __('Clue_state_name'), operate: 'LIKE'},
                        {field: 'clue_create_time', title: __('Clue_create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime',defaultValue:this.today(), title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '分配',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog',
                                    url: 'zhongzhuan/feiyu/xiansuo/assignkehu',
                                    extend: 'data-area=["40%","90%"]', //这是控制弹窗的弹出的尺寸
                                    callback: function (data) {
//                                        table.bootstrapTable('refresh');
//                                        Layer.alert(JSON.stringify(data));
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status > 0){
                                            return false;
                                        }
                                        return true;
                                        
                                    }
                                }
                            ]
                        
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        assignkehu: function () {
            Controller.api.bindevent();
        },
        //定义方法返回今日开始结束日期
        today: function () {
            const dd = new Date();
            const y = dd.getFullYear();// 获取今日年份
            const m = String(dd.getMonth() + 1).padStart(2, '0'); // 获取今日月份
            const d = String(dd.getDate()).padStart(2, '0'); // 获取今日日期

            const startOfDay = `${y}-${m}-${d} 00:00:00`;
            const endOfDay = `${y}-${m}-${d} 23:59:59`;

            return startOfDay + ' - ' + endOfDay;
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
